#include <stdio.h>
#include <stdlib.h>

typedef struct Point {
    int x;
    int y;
} POINT;

// 计算点到原点的距离的平方
int distanceSquared(struct Point p) {
    return p.x * p.x + p.y * p.y;
}

// 冒泡排序实现
void bubbleSort(struct Point points[], int n) {
    struct Point temp;
    for (int i = 0; i < n - 1; i++) {
        for (int j = i + 1; j < n; j++) {
            // 计算点到原点的距离的平方 如果找到距离更小的点，则交换位置
            if (distanceSquared(points[j]) < distanceSquared(points[i])) {
                // 交换
                temp = points[j];
                points[j] = points[i];
                points[i] = temp;
            }
        }
    }
}

int main(void) {
    int n;
    printf("Enter the number of points: ");
    scanf("%d", &n);

    // 动态分配内存以存储n个点
    struct Point *points = (struct Point *) malloc(n * sizeof(POINT));
    if (points == NULL) {
        fprintf(stderr, "Memory allocation failed\n");
        return 1;
    }

    // 输入点的坐标
    for (int i = 0; i < n; i++) {
        printf("Enter point %d (x y): ", i + 1);
        scanf("%d %d", &points[i].x, &points[i].y);
    }

    // 对点进行排序
    bubbleSort(points, n);

    // 输出排序后的点坐标
    for (int i = 0; i < n; i++) {
        printf("(%d,%d)", points[i].x, points[i].y);
    }

    // 释放动态分配的内存
    free(points);
    return 0;
}
